package cc.rengu.igas.channel.alipay.core.service.trans;


import cc.rengu.igas.channel.alipay.common.constant.AlipayParamConstant;
import cc.rengu.igas.channel.alipay.common.constant.AlipayServiceConstant;
import cc.rengu.igas.channel.alipay.common.constant.AlipayTreeNodeConstant;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.utility.util.RedisUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.Random;

public class NuccAliPayClientService extends RadpService {

    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil tree = new XmlTreeUtil();
        int retCode = 0;
        String idcTmp = null;
        //查询订单、撤销订单、退款、查询退款、关闭订单交易  从原交易查询IdcFlag保存到内部树，通讯前从内部树获取
        try {
            if (tree.isExist(AlipayTreeNodeConstant.nuccIdcFlag) && tree.getXmlTreeStringValue(AlipayTreeNodeConstant.nuccIdcFlag) != null) {
                idcTmp = tree.getXmlTreeStringValue(AlipayTreeNodeConstant.nuccIdcFlag);
            } else {
                //缓存获取idc列表
                String nuccIdc = RedisUtil.onceGet(AlipayParamConstant.NUCC_IDC_CAC);
                if (null == nuccIdc || nuccIdc.isEmpty()) {
                    rglog.error("从Redis中获取idc列表失败");
                    return -1;
                }
                JSONObject jsonObject = JSON.parseObject(nuccIdc);
                String idcNum = jsonObject.getString(AlipayTreeNodeConstant.nuccIdcNum);
                String nuccIdcValue = jsonObject.getString(AlipayTreeNodeConstant.nuccIdcList);
                rglog.info("网联IDC列表[{}]", nuccIdcValue);
                int iNuccIdcNum = Integer.parseInt(idcNum);
                String[] nuccIdcList = nuccIdcValue.split("\\|");
                if (nuccIdcList.length < iNuccIdcNum || iNuccIdcNum == 0) {
                    rglog.error("网联IDC个数[{}]	少于 网联IDC列表个数[{}]", iNuccIdcNum, nuccIdcList.length);
                    return -1;
                }
                //随机获取一个IDC
                Random randomId = new Random();
                idcTmp = nuccIdcList[randomId.nextInt(iNuccIdcNum)];
                rglog.info("idcTmp[{}]", idcTmp);
                tree.setXmlTreeStringValue(AlipayTreeNodeConstant.nuccIdcFlag, idcTmp);
            }
            //保存在数据库流水表REMARK1
            tree.setXmlTreeStringValue("remark1", idcTmp);
        } catch (Exception e) {
            rglog.error(e.getMessage());
            return -1;
        }

        String nuccCommSerName = AlipayServiceConstant.NUCC_ALIPAY_CLIENT_FOR_IDC + idcTmp;
        retCode = schedule.callService(nuccCommSerName, 0);
        if (retCode != 0) {
            rglog.error("callService [{}] error.retCode = [{}]", nuccCommSerName, retCode);
            return retCode;
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }

}
